//
//  UserAccount.swift
//  新浪微博(day01练习)
//
//  Created by KeY on 16/1/13.
//  Copyright © 2016年 KeY. All rights reserved.
//

import UIKit

class UserAccount: NSObject,NSCoding {

    var access_token: String?
    
    var expires_in: NSTimeInterval = 0 {
        didSet {
        
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //开发者测试有效期为5年 普通用户是3天
    var expires_date: NSDate?
    //当先授权用户的UID
    var uid: String?
    //用户名
    var name: String?
    //用户头像地址
    var avatar_large: String?
    
    //
    init(dict: [String : AnyObject]) {
    
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    //过滤key
//    override func setValue(value: AnyObject?, forKey key: String) {  }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { }
    //重写描述信息
    override var description: String {
        //使用KVC方式获取对象的字典信息
        let keys = ["access_token","expires_in","uid","name","avatar_large","expires_date"]
        let dict = self.dictionaryWithValuesForKeys(keys)
        return dict.description
    }
    //保存自定义对象到沙盒
    func saveAccount() {
        //拼接路径,XCode7.0中没有字符串拼接方法所以要转换成OC字符串进行拼接
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        //将对象保存到沙盒中
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
    }
    //获取磁盘中的归档对象()
    class func loadUserAccount() -> UserAccount? {
    
         let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount {
        
            if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
            
                return account
            }
        }
        return nil
    }
    //反归档:将磁盘中的二进制数据变成自定义对象 和反序列化类似
    required init?(coder aDecoder: NSCoder) {
        access_token
         = aDecoder.decodeDoubleForKey("access_token") as? String
        //当数据是基本数据类型时,可以直接解归档到具体类型
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
    }
    //归档:将自定义对象变成二进制数据保存到磁盘中和序列化类似
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
    }
    
    
    
    
    
    
    
    
    
    
    
}
